package com.example.chasshelper.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.chasshelper.R;
import com.example.chasshelper.dao.ClassroomDao;
import com.example.chasshelper.dao.CourseDao;
import com.example.chasshelper.dao.MessageDao;
import com.example.chasshelper.dao.RegistrationDao;
import com.example.chasshelper.dao.SolutionDao;
import com.example.chasshelper.dao.StudentDao;
import com.example.chasshelper.dao.TeacherDao;
import com.example.chasshelper.dao.TestDao;
import com.example.chasshelper.model.Classroom;
import com.example.chasshelper.model.Student;
import com.example.chasshelper.model.Teacher;
import com.example.chasshelper.model.Test;
import com.example.chasshelper.utils.DaoUtils;

import java.util.List;

public class TestAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContext;
    //数据
    private List<Test> list;
    private Teacher teacher;
    private Student student;
    //dao
    private TeacherDao teacherDao= DaoUtils.getTeacherDao();
    private StudentDao studentDao=DaoUtils.getStudentDao();
    private CourseDao courseDao=DaoUtils.getCourseDao();
    private ClassroomDao classroomDao=DaoUtils.getClassroomDao();
    private RegistrationDao registrationDao=DaoUtils.getRegistrationDao();
    private SolutionDao solutionDao = DaoUtils.getSolutionDao();
    private MessageDao messageDao = DaoUtils.getMessageDao();
    private TestDao testDao = DaoUtils.getTestDao();

    //该item的类型
    private String type;

    public String getType() {
        return type;
    }


    public TestAdapter(Context mContext, List<Test> list) {
        this.mContext = mContext;
        this.list = list;
    }

    /*
    * 确定item类型
    * */
    @Override
    public int getItemViewType(int position) {
        if (list.get(position).getTestType().equals("判断")) {
            return 1;
        }
        if (list.get(position).getTestType().equals("单选")) {
            return 2;
        }
        if (list.get(position).getTestType().equals("多选")) {
            return 3;
        }
        return super.getItemViewType(position);
    }

    /*
     * 绑定布局
     * */
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 1) {//判断
            JudgeViewHolder judgeViewHolder = new JudgeViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_test_rv_judge, parent, false));
            return judgeViewHolder;
        }
        if (viewType == 2) {//单选
            SingleChoiceViewHolder singleChoiceViewHolder = new SingleChoiceViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_test_rv_single_choice, parent, false));
            return singleChoiceViewHolder;
        }
        if (viewType == 3) {//多选
            MultipleChoiceViewHolder multipleChoiceViewHolder = new MultipleChoiceViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_test_rv_multiple_choice, parent, false));
            return multipleChoiceViewHolder;
        }
        return null;
    }

    /*
     * 设置每一个item
     * */
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Test test = list.get(position);
        if (holder instanceof JudgeViewHolder) {//判断只有两个选项
            ((JudgeViewHolder) holder).tv_title.setText(String.valueOf(position+1));
            ((JudgeViewHolder) holder).tv_content.setText("(判断题)"+test.getTestContext());
            ((JudgeViewHolder) holder).rb_option1.setText("A "+test.getTestItem1());
            ((JudgeViewHolder) holder).rb_option2.setText("B "+test.getTestItem2());
            ((JudgeViewHolder) holder).tv_type.setText("判断");
            ((JudgeViewHolder) holder).tv_type.setVisibility(View.INVISIBLE);
        }
        else if (holder instanceof SingleChoiceViewHolder) {//单选有3-5个选项
            ((SingleChoiceViewHolder) holder).tv_title.setText(String.valueOf(position+1));
            ((SingleChoiceViewHolder) holder).tv_content.setText("(单选题)"+test.getTestContext());
            ((SingleChoiceViewHolder) holder).rb_option1.setText("A "+test.getTestItem1());
            ((SingleChoiceViewHolder) holder).rb_option2.setText("B "+test.getTestItem2());
            if (Integer.valueOf(test.getTestItemNum()) >= 4) {
                ((SingleChoiceViewHolder) holder).rb_option4.setText("D "+test.getTestItem4());
            } else {
                ((SingleChoiceViewHolder) holder).rb_option4.setVisibility(View.INVISIBLE);
            }
            if (Integer.valueOf(test.getTestItemNum()) >= 5) {
                ((SingleChoiceViewHolder) holder).rb_option5.setText("E "+test.getTestItem5());
            } else {
                ((SingleChoiceViewHolder) holder).rb_option5.setVisibility(View.INVISIBLE);
            }
            ((SingleChoiceViewHolder) holder).tv_type.setText("单选");
            ((SingleChoiceViewHolder) holder).tv_type.setVisibility(View.INVISIBLE);

        }
        else if (holder instanceof MultipleChoiceViewHolder) {//多选有3-5个选项
            ((MultipleChoiceViewHolder) holder).tv_title.setText(String.valueOf(position+1));
            ((MultipleChoiceViewHolder) holder).tv_content.setText("(多选题)"+test.getTestContext());
            ((MultipleChoiceViewHolder) holder).cb_option1.setText("A "+test.getTestItem1());
            ((MultipleChoiceViewHolder) holder).cb_option2.setText("B "+test.getTestItem2());
            ((MultipleChoiceViewHolder) holder).cb_option3.setText("C "+test.getTestItem3());
            if (Integer.valueOf(test.getTestItemNum()) >= 4) {
                ((MultipleChoiceViewHolder) holder).cb_option4.setText("D "+test.getTestItem4());
            } else {
                ((MultipleChoiceViewHolder) holder).cb_option4.setVisibility(View.INVISIBLE);
            }
            if (Integer.valueOf(test.getTestItemNum()) >= 5) {
                ((MultipleChoiceViewHolder) holder).cb_option5.setText("E "+test.getTestItem5());
            } else {
                ((MultipleChoiceViewHolder) holder).cb_option5.setVisibility(View.INVISIBLE);
            }
            ((MultipleChoiceViewHolder) holder).tv_type.setText("多选");
            ((MultipleChoiceViewHolder) holder).tv_type.setVisibility(View.INVISIBLE);

        }
    }

    /*
     * 返回item数量
     * */
    @Override
    public int getItemCount() {
        return list.size();
    }


    /*
     * item布局
     * */
    class JudgeViewHolder extends RecyclerView.ViewHolder {

        //控件
        TextView tv_title;
        TextView tv_content;
        RadioGroup rg_option;
        RadioButton rb_option1;
        RadioButton rb_option2;
        TextView tv_type;
        public JudgeViewHolder(@NonNull View itemView) {
            super(itemView);
            //绑定控件
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_content = itemView.findViewById(R.id.tv_content);
            rg_option = itemView.findViewById(R.id.rg_option);
            rb_option1 = itemView.findViewById(R.id.rb_option1);
            rb_option2 = itemView.findViewById(R.id.rb_option2);
            tv_type = itemView.findViewById(R.id.tv_type);
        }
    }

    class SingleChoiceViewHolder extends RecyclerView.ViewHolder {
        //控件
        TextView tv_title;
        TextView tv_content;
        RadioGroup rg_option;
        RadioButton rb_option1;
        RadioButton rb_option2;
        RadioButton rb_option3;
        RadioButton rb_option4;
        RadioButton rb_option5;
        TextView tv_type;
        public SingleChoiceViewHolder(@NonNull View itemView) {
            super(itemView);
            //绑定控件
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_content = itemView.findViewById(R.id.tv_content);
            rg_option = itemView.findViewById(R.id.rg_option);
            rb_option1 = itemView.findViewById(R.id.rb_option1);
            rb_option2 = itemView.findViewById(R.id.rb_option2);
            rb_option3 = itemView.findViewById(R.id.rb_option3);
            rb_option4 = itemView.findViewById(R.id.rb_option4);
            rb_option5 = itemView.findViewById(R.id.rb_option5);
            tv_type = itemView.findViewById(R.id.tv_type);
        }
    }

    class MultipleChoiceViewHolder extends RecyclerView.ViewHolder {
        //控件
        TextView tv_title;
        TextView tv_content;
        RadioGroup rg_option;
        CheckBox cb_option1;
        CheckBox cb_option2;
        CheckBox cb_option3;
        CheckBox cb_option4;
        CheckBox cb_option5;
        TextView tv_type;
        public MultipleChoiceViewHolder(@NonNull View itemView) {
            super(itemView);
            //绑定控件
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_content = itemView.findViewById(R.id.tv_content);
            rg_option = itemView.findViewById(R.id.rg_option);
            cb_option1 = itemView.findViewById(R.id.cb_option1);
            cb_option2 = itemView.findViewById(R.id.cb_option2);
            cb_option3 = itemView.findViewById(R.id.cb_option3);
            cb_option4 = itemView.findViewById(R.id.cb_option4);
            cb_option5 = itemView.findViewById(R.id.cb_option5);
            tv_type = itemView.findViewById(R.id.tv_type);
        }
    }


}
